//
//  UIView+Tool.m
//  SLDemo
//
//  Created by bocai on 2016/12/2.
//  Copyright © 2016年 Heracles. All rights reserved.
//

#import "UIView+Tool.h"

@implementation UIView (Tool)

- (CGFloat)t_x
{
    return CGRectGetMinX(self.frame);
}

- (void)setT_x:(CGFloat)t_x
{
    CGRect frame = self.frame;
    frame.origin.x = t_x;
    self.frame = frame;
}

- (CGFloat)t_y
{
    return CGRectGetMinY(self.frame);
}

- (void)setT_y:(CGFloat)t_y
{
    CGRect frame = self.frame;
    frame.origin.y = t_y;
    self.frame = frame;
}

- (CGFloat)t_maxX
{
    return CGRectGetMaxX(self.frame);
}

- (void)setT_maxX:(CGFloat)t_maxX
{
    CGRect frame = self.frame;
    frame.origin.x = t_maxX - CGRectGetWidth(self.frame);
    self.frame = frame;
}

- (CGFloat)t_maxY
{
    return CGRectGetMaxY(self.frame);
}

- (void)setT_maxY:(CGFloat)t_maxY
{
    CGRect frame = self.frame;
    frame.origin.y = t_maxY - CGRectGetHeight(self.frame);
    self.frame = frame;
}

- (CGFloat)t_width
{
    return CGRectGetWidth(self.frame);
}

- (void)setT_width:(CGFloat)t_width
{
    CGRect frame = self.frame;
    frame.size.width = t_width;
    self.frame = frame;
}

- (CGFloat)t_height
{
    return CGRectGetHeight(self.frame);
}

- (void)setT_height:(CGFloat)t_height
{
    CGRect frame = self.frame;
    frame.size.height = t_height;
    self.frame = frame;
}

- (CGPoint)t_origin
{
    return self.frame.origin;
}

- (void)setT_origin:(CGPoint)t_origin
{
    CGRect frame = self.frame;
    frame.origin = t_origin;
    self.frame = frame;
}

- (CGSize)t_size
{
    return self.frame.size;
}

- (void)setT_size:(CGSize)t_size
{
    CGRect frame = self.frame;
    frame.size = t_size;
    self.frame = frame;
}

- (UIViewController *)t_viewController
{
    for (UIView *view = self; view; view = view.superview) {
        __kindof UIResponder *nextResponder = [view nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return nextResponder;
        }
    }
    return nil;
}

- (void)t_addSubviews:(NSArray *)subviews
{
    for (UIView *view in subviews) {
        [self addSubview:view];
    }
}

+ (UINib *)t_nib
{
    return [UINib nibWithNibName:NSStringFromClass(self) bundle:nil];
}

+ (__kindof UIView *)t_nibView
{
    return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}

- (void)t_filletWithHalfHeight
{
    [self t_filletWithRadius:self.t_height/2.0];
}

- (void)t_filletWithHalfWidth
{
    [self t_filletWithRadius:self.t_width/2.0];
}

- (void)t_filletWithRadius:(CGFloat)radius
{
    self.layer.cornerRadius = radius;
    self.layer.masksToBounds = YES;
}

- (void)t_borderWithWidth:(CGFloat)width
                    color:(UIColor *)color
{
    self.layer.borderWidth = width;
    self.layer.borderColor = color.CGColor;
}

- (void)t_addToWindow
{
    UIWindow *window = [UIApplication sharedApplication].windows.lastObject;
    [window addSubview:self];
}

@end
